package sketch.seg.cd.exp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import sketch.doc.Sketch;
import sketch.doc.SketchContainer;
import sketch.ink.SampledPoint;
import sketch.ink.Stroke;

public class MergePrinter {

	public static final String _in = "./exp/zhengwt/zhengwt.seg.";

	public static final String _out = "./exp/zhengwt.";

	private String in_pre;

	private String output;

	public MergePrinter(int file_count) {
		String num = Integer.toString(file_count);
		this.in_pre = _in + num;
		this.output = _out + num + ".dat";
	}

	public void print() {

		int RR_NUM = 20;
		int SS_NUM = 7;
		BufferedReader br_rr[] = new BufferedReader[RR_NUM];
		BufferedReader br_ss[] = new BufferedReader[SS_NUM];

		for (int i = 1; i <= RR_NUM; i++) {
			String input = in_pre + ".k" + Integer.toString(i);
			try {
				br_rr[i-1] = new BufferedReader(new InputStreamReader(new FileInputStream(new File(input))));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		for (int i = 1; i <= SS_NUM; i++) {
			String input = in_pre + "_" + Integer.toString(2*i+1) + ".output";
			try {
				br_ss[i-1] = new BufferedReader(new InputStreamReader(new FileInputStream(new File(input))));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(output))));

			bw.write("# generated data file - by MergePrinter (zhenwgt)\n");
			bw.write("# index | relative rho (1 - 20) | rho (3, 5, 7, 9, 11, 13, 15)\n\n");

			int count = 1;
			while (true) {
				String tmp = br_rr[0].readLine();
				if (tmp == null) {
					bw.write("# end of file\n");
					break;
				}

				bw.write(Integer.toString(count));
				bw.write("\t");
				bw.write(tmp);
				bw.write("\t");

				for (int i = 1; i < br_rr.length; i++) {
					tmp = br_rr[i].readLine();
					if (tmp == null)
						tmp = "0";

					bw.write(tmp);
					bw.write("\t");
				}

				for (int i = 0; i < br_ss.length; i++) {
					tmp = br_ss[i].readLine();
					if (tmp == null)
						tmp = "0";

					bw.write(tmp);
					bw.write("\t");
				}

				count++;
				bw.write("\n");
			}

			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {

		for (int i = 1; i <= 20; i++) {
			new MergePrinter(i).print();
		}
	}
}
